﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace OnlineDigitalCameraWebsite.Class
{
    public class Invoice
    {
        

        private ArrayList _invoiceItems = new ArrayList();
        public ArrayList InvoiceItems
        {
            get
            {
                return _invoiceItems;
            }
            set
            {
                _invoiceItems = value;
            }
        }

        private string _currency = "USD"; //currency to display in
        public string Currency
        {
            get
            {
                return _currency;
            }
            set
            {
                if (value == null || value.Trim() == "")
                {
                    _currency = "USD";
                }
                else
                {
                    _currency = value.ToUpper();
                }
            }
        }


        private string _invoiceId = "";
        /// <summary>
        /// Same as session Id (unless pulling from database)
        /// </summary>
        public string InvoiceId
        {
            get
            {
                return _invoiceId;
            }
            set
            {
                _invoiceId = value;
            }
        }
        public string ProductNames
        {
            get
            {
                string productNames = "";
                foreach (InvoiceItem x in _invoiceItems)
                {
                    productNames += x.ProductName.Trim() + ", ";

                }
                if (productNames.Length > 2)
                {
                    productNames = productNames.Substring(0, productNames.Length - 2);
                }
                return productNames;
            }
        }

         
        /// <summary>
        /// Cost of Unit Prices * qty
        /// </summary>
        public decimal SubTotal
        {
            get
            {
                decimal subtotal = 0.0M;
                foreach (InvoiceItem x in _invoiceItems)
                {
                    subtotal += (x.UnitPrice * x.Quantity);
                }
                return subtotal;
            }
        }
        

        public decimal Total
        {
            get
            {
                return this.SubTotal;
            }
        }
       

     
        public Invoice()
        {
            //
            // TODO: Add constructor logic here
            //

        }


        public Invoice(string invoiceId)
        {
            this.InvoiceId = invoiceId;
        }

        public void EmptyCart()
        {
            _invoiceItems = new ArrayList();
        }

        /// <summary>
        /// Adding another item to the cart (may be one there already - dont know)
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="qty"></param>
        public void AddToInvoice(int productId, int qty, string _im,string category)
        {
            bool foundInvoiceItem = false;

            foreach (InvoiceItem x in _invoiceItems)
            {
                if (x.ProductId == productId)
                {
                    x.Quantity += qty;
                    if (x.Quantity <= 0)
                    {
                        RemoveProduct(productId);
                    }
                    foundInvoiceItem = true;
                    break;
                }
            }

            if (!foundInvoiceItem && qty > 0)
            {
                InvoiceItem invItem = new InvoiceItem(this.InvoiceId, productId, qty, _im ,category);
                _invoiceItems.Add(invItem);
            }
        }



        /// <summary>
        /// Updating Invoice Quantity
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="qty"></param>
        public void UpdateProductQuantity(int productId, int qty)
        {
            if (qty <= 0)
            {
                qty = 0;
                RemoveProduct(productId);
            }
            else
            {
                foreach (InvoiceItem x in _invoiceItems)
                {
                    if (x.ProductId == productId)
                    {
                        x.Quantity = qty;
                        break;
                    }
                }
            }
        }

        public void RemoveProduct(int productId)
        {
            foreach (InvoiceItem x in _invoiceItems)
            {
                if (x.ProductId == productId)
                {
                    InvoiceItems.Remove(x);
                    break;
                }
            }
        }

    }
}